//
//  XMQQFrame.m
//  XMKit
//
//  Created by chenming on 2017/12/21.
//  Copyright © 2017年 ChenXiaoMing. All rights reserved.
//

#import "XMQQFrame.h"
#import "XMQQModel.h"

#define MZNameFont 15 // 昵称字体的大小

#define MZTextFont 14 // 内容字体的大小

@implementation XMQQFrame

/**
 * 计算文字的大小
 */
- (CGSize)sizeWithText:(NSString *)text maxSize:(CGSize)maxSize fontSize:(CGFloat)fontSize {
    //计算文本的大小
    CGSize nameSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil].size;
    return nameSize;
}

- (void)setQqModel:(XMQQModel *)qqModel{
    _qqModel = qqModel;
    CGFloat margin = 10;
    //头像
    CGFloat iconW = 40;
    CGFloat iconH = 40;
    CGFloat iconX = margin;
    CGFloat iconY = margin;
    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    //昵称
    CGSize nameSize = [self sizeWithText:self.qqModel.name maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) fontSize:MZNameFont];
    CGFloat nameX = CGRectGetMaxX(_iconFrame) + margin;
    CGFloat nameY = iconY;
    _nameFrame = CGRectMake(nameX, nameY, nameSize.width, nameSize.height);
    //时间
    CGSize timeSize = [self sizeWithText:self.qqModel.time maxSize:CGSizeMake(kSCREEN_WIDTH - CGRectGetMidX(_pictureFrame), MAXFLOAT) fontSize:12];
    CGFloat timeX = nameX;
    CGFloat timeY = CGRectGetMaxY(_nameFrame) + margin;
    _timeFrame = CGRectMake(timeX, timeY, timeSize.width, timeSize.height);
    //达人
    CGFloat expertW = 14;
    CGFloat expertH = 14;
    CGFloat expertY = nameY;
    CGFloat expertX = CGRectGetMaxX(_nameFrame) + margin;
    _expertFrame = CGRectMake(expertX, expertY, expertW, expertH);
    //QQ内容
    CGSize textSize = [self sizeWithText:self.qqModel.text maxSize:CGSizeMake(kSCREEN_WIDTH - 2 * margin, MAXFLOAT) fontSize:MZTextFont];
    CGFloat textX = iconX;
    CGFloat textY = CGRectGetMaxY(_iconFrame) + margin;
    _textFrame = CGRectMake(textX, textY, textSize.width, textSize.height);
    
    //QQ图片 （这里的图片大小给定了100，实际根据自己需求调整）
    if (self.qqModel.picture) {
        CGFloat pictureW = 100;
        CGFloat pictureH = 100;
        CGFloat pictureX = iconX;
        CGFloat pictureY = CGRectGetMaxY(_textFrame) + margin;
        _pictureFrame = (CGRect){{pictureX,pictureY},{pictureW,pictureH}};
        _rowHeight = CGRectGetMaxY(_pictureFrame) + margin;
    }else{
        _rowHeight = CGRectGetMaxY(_textFrame) + margin;
    }
    
}

@end
